var form;
var table;
var layer;
var upload;
layui.use(['layer','form','table','upload'],function(){
    form = layui.form;
    table=layui.table;
    layer=layui.layer;
    upload=layui.upload;

    //轮播图信息
    selCarousel();


    //修改轮播图上线\下线
    form.on('switch', function(data){
        var flag=data.elem.checked;

        //获取优惠券状态
        var display=null;
        if(flag){
            display=2;
        }else{
            display=3;
        }

        //轮播图id
        var carousel_id=data.elem.value;
        $.ajax({
            url: "/SyOperationController/updCarouselDisplay",
            dataType: "json",
            type:"post",
            data:{"display":display,"carousel_id":carousel_id},
            success: function (data) {
                if(data.code==0){
                    layer.msg('修改成功');
                }else{
                    layer.msg('修改失败');
                }
            }
        });
    });


});



//轮播图数据表格
function selCarousel(){
    var img_title=$("#img_title").val();
    var display=$("#display").val();
    var img_type=$("#img_type").val();
    var serial_number=$("#serial_number").val();
    var start_date=$("#start_date").val();
    var expire_date=$("#expire_date").val();
    console.log(img_title);
    console.log(display);
    console.log(img_type);
    console.log(serial_number);
    console.log(start_date);
    $.ajax({
        url: "/SyOperationController/selCarousel",
        type: "post",
        dataType: "json",
        data:{"img_title":img_title,"display":display,"img_type":img_type,"serial_number":serial_number,"start_date":start_date,"expire_date":expire_date},
        success: function (data) {
            console.log(data);
            getTable(data.obj);
        }
    })
}

//商品数据表格
function getTable(data){
    var tb1;
    tb1 = table.render({
        elem: "#table_id",
        title: "轮播图列表",
        limit: 5,
        data: data,
        page: true,
        cols: [[
            {type: 'checkbox', width: 60},
            {field:"serial_number",title:'编号',width: 100, align: 'center'},
            {field: 'img_title', title: '名称', width: 150, align: 'center'},
            {field: 'img_type', title: '图片类型', width: 150, align: 'center', templet: function (d) {
                    if(d.img_type==1){
                        return "商品";
                    }else if(d.img_type==2){
                        return "视频";
                    }else{
                        return "异常";
                    }
                }},
            {field: 'url', title: '课程图片', width: 180, align: 'center', templet: function (d) {
                    return "<img src=http://182.92.209.121:/"+d.img_address+" title='"+d.course_name+"' width='100' height='70'>"
                }},
            {field:'start_date',title:'时间',width:250,align:'center',templet:function(d){
                    var start_date=d.start_date;
                    start_date = start_date.slice(0, start_date.indexOf("."));
                    start_date = start_date.replaceAll('T',' ');

                    var expire_date=d.expire_date;
                    expire_date = expire_date.slice(0, expire_date.indexOf("."));
                    expire_date = expire_date.replaceAll('T',' ');
                    return "<p>开始时间："+start_date+"</p><p>结束时间："+expire_date+"</p>";
                }},
            {field: 'display', title: '上线/下线', width: 120, align: 'center', templet: function (d) {
                    if (d.display == 2) {
                        return "<input type='checkbox' checked=''  name='open' lay-skin='switch' lay-filter='switchTest' value=\"" + d.carousel_id + "\">"
                    } else if (d.display == 3) {
                        return "<input type='checkbox' name='close' lay-skin='switch' value=\"" + d.carousel_id + "\">"
                    }
                }
            },
            {field:'',title:'操作',width:150,align:'center',templet:function(d){
                    return "<span ><a  onclick='javascript:updCarousel(\""+d.carousel_id+"\")' style='color:#1ABC9C;'>编辑</a></span>"+
                        "<span style='margin-left: 20px'><a  onclick='javascript:delCarousel(\""+d.carousel_id+"\")' style='color:#1ABC9C;'>删除</a></span>"
                }}
        ]]
    });

}

//刷新
function refresh() {
    location.reload();
}
//返回
function go() {
    history.go(-1)
}

//点击查检索页面
function retrieve() {
    //点击检索之后清空输入框
    $("input").val("");
    type: "post",
        $(this).find("option:selected").removeAttr("selected");

    layer.open({
        type : 2,
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>高级检索</div>",
        area : [ '670px', '500px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CarouselRetrieve.html",
        success:function(){
        }
    });
    window.layer_1=layer;
}


//点击编辑
function updCarousel(carousel_id){
    location.href="CarouselUpdate.html?"+carousel_id;
}

//弹出修改页面
function  CarouselUpdateLayer(data) {
    layer.open({
        type : 2,//弹出层的类型
        id:1,
        title : "<div style='color:#666666;font-weight: 600;background: #F8F8F8;'>修改课程</div>",
        area : [ '670px', '580px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"CarouselUpdate.html",
        success:function(layero,index){
            var obj=layero.find('iframe').contents();
            obj.find('input[name=imgTitle]').val(data.img_title);
            obj.find('input[name=startDate]').val(data.start_date);
            obj.find('input[name=expireDate]').val(data.expire_date);
            obj.find('input[name=imgAddress]').val(data.img_address);
            obj.find('#remarks').val(data.remarks);
        }
    });
    window.layer_1=layer;
}

//删除轮播图
function delCarousel(carousel_id) {
    $.ajax({
        url: "/SyOperationController/delCarousel",
        type: "post",
        dataType: "json",
        data:{"carousel_id":carousel_id},
        success: function (data) {
            if(data.code==0){
                selCarousel();
                layer.msg('删除成功')
            }else{
                layer.msg('删除失败')
            }
        }
    });
}

//添加轮播图
function addCarousel() {
    location.href="CarouselAdd.html";
}

